//
//  CrowdVoiceItemViewModel.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/25.
//

import Foundation
//系统音色
class SystemVoiceItemViewModel : BaseViewModel {
    
    func doVoice(bean: AiVoiceBean) {
//        if AudioPlayerManager.shared.isPlaying() {
//            showToast("正在播放中，请稍后")
//            return
//        }
        request {
            let text = configManager.getConfig().aiChat.aiVoiceTestStr
            let (hexString, hexData) = try await AiChatExt.doAiVoiceTest(text: text, voiceId: bean.voiceId, voiceHex: bean.voiceHex)
            if ((bean.voiceHex ?? "").isEmpty) {
                bean.voiceHex = hexString
                //保存数据
                try await LeancloudManager.instance.save(todo: bean)
            }
            try check(hexData != nil, "播放失败")
            return hexData!
        } suc: {
            AudioPlayerManager.shared.playData(from: $0)
        } fail: {
            showToast($0.message)
        }
        
    }
    
    deinit {
        AudioPlayerManager.shared.pause()
    }
}
